<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>欢迎页</title>
    <link rel="stylesheet" href="../../static/frame/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/frame/static/css/style.css">
    <link rel="icon" href="../../static/frame/static/image/code.png">
</head>
<body class="body">

<div class="layui-row layui-col-space10 my-index-main">
    <table class="layui-hide" id="agentlist" lay-filter="agentlist"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>

</div>

<script src="../../static/jquery-3.3.1.js"></script>
<script type="text/javascript" src="../../static/frame/layui/layui.js"></script>
<script type="text/javascript" src="../../static/js/index.js"></script>
<script type="text/javascript" src="../../static/frame/echarts/echarts.min.js"></script>
<script type="text/javascript">


    layui.use(['form', 'table', 'element', 'layer', 'vip_tab'], function () {
        // 打开选项卡
        $('.my-nav-btn').on('click', function () {
            if ($(this).attr('data-href')) {
                vipTab.add($(this), '<i class="layui-icon">' + $(this).find("button").html() + '</i>' + $(this).find('p:last-child').html(), $(this).attr('data-href'));
            }
        });
        var table = layui.table, form = layui.form;
        table.render({
            elem: '#agentlist'
            , url: '../ajax.php?mod=getagentlist'
            , cellMinWidth: 130
            , cols: [[
                {field: 'ID', title: 'ID', width: 60}
                , {field: 'username', title: '账号', edit: 'text'}
                , {field: 'password', title: '密码', edit: 'text'}
                , {field: 'begintime', title: '开通时间'}
                , {field: 'logintime', title: '上次登录'}
                , {field: 'loginip', title: '登录IP'}
                , {field: 'levelname', title: '代理级别'}
                , {field: 'money', title: '余额'}
                , {field: 'allspend', title: '累计消费'}
                , {field: 'allrecharge', title: '累计充值'}
                , {field: 'qq', title: 'QQ', edit: 'text'}
                , {field: 'status', title: '状态'}
                , {toolbar: '#barDemo', title: '操作', width: 240}
            ]]
            , page: true
        });
        table.on('tool', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除这个代理么？', function (index) {
                    $.ajax({
                        url: '../ajax.php?mod=delagent',
                        type: 'POST',
                        dataType: 'html',
                        data: 'aid=' + data.ID,
                        success: function (data) {
                            //layer.close(loading);
                            obj.del();
                            layer.msg(data);
                        },
                        error: function (data) {
                            //layer.close(loading);
                            layer.msg('请求失败' + data);
                        }
                    });
                    layer.close(index);
                });
            }
        });
        table.on('edit(agentlist)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            //layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
            $.ajax({
                url: '../ajax.php?mod=changeagent',
                type: 'POST',
                dataType: 'html',
                data: 'aid=' + obj.data.ID + '&mod=' + obj.field + '&value=' + obj.value,
                success: function (data) {
                    //obj.del();
                    //layer.close(loading);
                    layer.msg(data);
                },
                error: function (data) {
                    //layer.close(loading);
                    layer.msg('请求失败' + data);
                }
            })
        });
        form.on('switch', function (obj) {
            $.ajax({
                url: '../ajax.php?mod=changeagent',
                type: 'POST',
                dataType: 'html',
                data: 'aid=' + this.value + '&mod=' + this.name + '&value=' + obj.elem.checked,
                success: function (data) {
                    //layer.close(loading);
                    layer.msg(data);
                },
                error: function (data) {
                    //layer.close(loading);
                    layer.msg('请求失败' + data);
                }
            })
        });

    });


</script>
</body>
</html>